package com.shutuo.callnummeiwei.data;

import org.json.JSONException;
import org.json.JSONObject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import ashy.earl.common.data.JsonParser;

public class Parser {


    public static final JsonParser PARSER_STRING = new JsonParser<MsgString>() {
        @Nullable
        @Override
        public MsgString onParse(@NonNull JSONObject json) {

            return new MsgString(json.optInt("bigTable"), json.optInt("inTable"), json.optInt("smallTable")
                    , json.optInt("bigCTNumber"), json.optInt("inCTNumber"), json.optInt("smallCTNumber")
                    , json.optInt("bigWaitTable"), json.optInt("inWaitTable"), json.optInt("smallWaitTable")
            );
        }

        @Nullable
        @Override
        public JSONObject toJson(@NonNull MsgString obj) {
            try {
                return new JSONObject().put("bigTable", obj.bigTable).put("inTable", obj.inTable).put("smallTable", obj.smallTable)
                        .put("bigCTNumber", obj.bigCTNumber).put("inCTNumber", obj.inCTNumber).put("smallCTNumber", obj.smallCTNumber)
                        .put("bigWaitTable", obj.bigWaitTable).put("inWaitTable", obj.inWaitTable).put("smallWaitTable", obj.smallWaitTable);
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public MsgString[] newArray(int size) {
            return new MsgString[size];
        }
    };

    public static final JsonParser PARSER_WAITMSGSTRING = new JsonParser<WaitMsgString>() {

        @Nullable
        @Override
        public WaitMsgString onParse(@NonNull JSONObject jsonObject) {
            return new WaitMsgString(jsonObject.optInt("waitTable"), jsonObject.optInt("callTable"));
        }

        @Nullable
        @Override
        public JSONObject toJson(@NonNull WaitMsgString waitMsgString) {
            try {
                return new JSONObject().put("waitTable", waitMsgString.waitTable).put("callTable", waitMsgString.callTable);
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public WaitMsgString[] newArray(int i) {
            return new WaitMsgString[i];
        }
    };

}
